import { createAction } from 'redux-actions'
import { UPDATEPAGESELFCENTER } from '../types/pageselfcenter'
import { getArchiveInfoRequest } from '../../../../service/user-info-controller'
import { jobRecommendRequest } from '../../../../service/post-controller'
import { changeDefault } from '../../../../service/org-company-controller'

const formatdata = entry => {
  return {
    archiveComplete: entry.archiveComplete, // 完成度
    userName: entry.userName, // 完成度
    viewCounts: entry.viewCounts, // 浏览量
    shareCounts: entry.shareCounts, // 收藏量
    jobStatus: entry.jobStatus, // 求职意向
    archiveId: entry.archiveId // archiveId
  }
}

export const DOREQUESTSELFCENTER = createAction(UPDATEPAGESELFCENTER, ({ userId }) => {
  return new Promise(resolve => {
    getArchiveInfoRequest({ userId }).then(({ data }) => {
      const { networkerror, entry, status, message } = data
      if (networkerror) {
        resolve({
          networkerror: true
        })
      } else {
        if (status && entry) {
          let dataformat = formatdata(entry)
          resolve({
            networkerror: false,
            showEmpty: false,
            ...dataformat
          })
        } else {
          resolve({
            networkerror: false,
            showEmpty: true
          })
        }
      }
    })
  })
})

export const DOREQUESTRECOMMON = createAction(UPDATEPAGESELFCENTER, ({ city }) => {
  return new Promise(resolve => {
    jobRecommendRequest({ city }).then(({ data }) => {
      const entry = (data && data.entry) || []
      console.log(entry, '岗位')
      resolve({
        showEmptyRecommon: !(entry && entry.length),
        dataSourceRecommon: entry
      })
    })
  })
})
export const DOREQUESTCHANGECP = createAction(UPDATEPAGESELFCENTER, payload => {
  return new Promise(resolve => {
    changeDefault({ ...payload }).then(({ data }) => {
      const {
        data: { member, post },
        code,
        message
      } = data
      resolve({
        status: code === 200,
        entry: member && Object.keys(member).length > 0 ? !!(member.remainingDay > 0) : false,
        memberType: (member && member.memberType) || '',
        message: message || '',
        postData: post
      })
    })
  })
})
